package com.yeyuanqi.hottop.untils;

import com.yeyuanqi.hottop.service.RedisService;
import com.yeyuanqi.hottop.vo.NameAndValueVO;
import com.yeyuanqi.hottop.vo.StackedLineVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 封装了一个用于生成七天每日top1信息数据的方法,并将数据转为折现堆叠图格式
 * @author yeyuanqi
 * @date 2022/01/19
 **/
@Component
public class StackedLineChartDataUtil {

    @Autowired
    private RedisService redisService;

    /**
     * 从redis获取的每日热搜top1数据转换成list<StackedLineVO>类型
     * 用于折线图堆叠
     * @return 返回一个List<StackedLineVO>列表
     */
    public StackedLineVO getTopOneDataForSevenDays(){
        String date;
        StackedLineVO stackedLineVO = new StackedLineVO();
        List<String> dateList = new ArrayList<>();
        List<NameAndValueVO> bddataList = new ArrayList<>();
        List<NameAndValueVO> jrdataList = new ArrayList<>();
        List<NameAndValueVO> txdataList = new ArrayList<>();
        List<NameAndValueVO> wbdataList = new ArrayList<>();
        //获取现在的日期
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date nowdate = new Date();
        Calendar calendar = Calendar.getInstance();//new一个Calendar类,把Date放进去
        calendar.setTime(nowdate);
        //获取七天内所有平台top1的数据
        for (int i=0;i<7;i++) {
            //此时的日期从六天前到今天共七天
            //获取每天的日期，yyyy-mm-dd格式
            if (i==0){
                calendar.add(Calendar.DATE, 0);
                date = df.format(calendar.getTime());
            } else {
                calendar.add(Calendar.DATE, -1);
                date = df.format(calendar.getTime());
            }

            //封装时间列表
            dateList.add(date);

            //从redis中获取top1的数据
            String bdrediskey = date + "-baidudailytop";
            NameAndValueVO bddata = redisService.zGetMaxScore(bdrediskey);
            bddataList.add(bddata);

            String jrrediskey = date + "-jinridailytop";
            NameAndValueVO jrdata = redisService.zGetMaxScore(jrrediskey);
            jrdataList.add(jrdata);

            String txrediskey = date + "-tengxundailytop";
            NameAndValueVO txdata = redisService.zGetMaxScore(txrediskey);
            txdataList.add(txdata);

            String wbrediskey = date + "-weibodailytop";
            NameAndValueVO wbdata = redisService.zGetMaxScore(wbrediskey);
            wbdataList.add(wbdata);
        }

        //封装数据
        stackedLineVO.setHotdate(dateList);
        stackedLineVO.setBddata(bddataList);
        stackedLineVO.setJrdata(jrdataList);
        stackedLineVO.setTxdata(txdataList);
        stackedLineVO.setWbdata(wbdataList);

        return stackedLineVO;
    }
}
